package com.yanqu.road.logic.cross;

import com.yanqu.road.entity.rank.cross.CrossUnionActivityRank;
import com.yanqu.road.logic.bussiness.rank.RankBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.RankListHelper;
import com.yanqu.road.logic.pb.CrossActivityPb;
import com.yanqu.road.logic.sort.CrossUnionActivityRankSort;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;
import java.util.stream.IntStream;

public class CrossUnionActivityRankListModel {

	private int rankNum = GameConfig.RANK_DEFAULT_MAX_COUNT;

	private CrossUnionActivityRankSort sorter;

	private LinkedList<CrossUnionActivityRank> rankList;

	private int activityId;

	private boolean isUpdate = false;

	private boolean isNeedSync = false;

	private CrossActivityProto.CrossActivityUnionRankListMsg.Builder rankListMsg;

	public void setRankNum(int rankNum) {
		this.rankNum = rankNum;
	}

	public int getActivityId() {
		return activityId;
	}

	public void setActivityId(int activityId) {
		this.activityId = activityId;
	}

	public LinkedList<CrossUnionActivityRank> getRankList() {
		return rankList;
	}

	public CrossUnionActivityRankListModel(int activityId) {
		sorter = new CrossUnionActivityRankSort();
		rankList = new LinkedList<>();
		this.activityId = activityId;
		rankListMsg = CrossActivityProto.CrossActivityUnionRankListMsg.newBuilder();
	}

	public void initRankList() {
		rankList = new LinkedList<>();
	}

	private void initRankMsg() {
		ArrayList<CrossUnionActivityRank> crossUnionActivityRanks = new ArrayList<>(rankList);
		ArrayList<CrossUnionActivityRank> tempList = new ArrayList<>();
		for (CrossUnionActivityRank rank : crossUnionActivityRanks) {
			if (!rank.isDel()) {
				tempList.add(rank);
			}
		}
		rankListMsg = CrossActivityPb.parseUnionRankListMsg(tempList);
		rankListMsg.setActivityId(activityId);
	}

	public void setRankList(LinkedList<CrossUnionActivityRank> unionRankList) {
		synchronized (this) {
			rankList = new LinkedList<>(unionRankList);
			RankListHelper.checkUnionActivityRankListValid(rankList);       //校验数组
			rankList.sort(sorter);
			initRankMsg();
		}
	}

	public CrossActivityProto.CrossActivityUnionRankListMsg.Builder getRankListMsg() {
		return rankListMsg;
	}

	public boolean isNeedSync() {
		return isNeedSync;
	}

	public void setNeedSync(boolean needSync) {
		isNeedSync = needSync;
	}

	//领取排行奖励一定要用加锁的
	public int getMyRank(String unionUid) {
		synchronized (this) {
			return unlockGetMyRank(unionUid);
		}
	}

	//排行榜排名可以不加锁
	public int unlockGetMyRank(String unionUid) {
		OptionalInt result = IntStream.range(0, rankList.size())
				.filter(i -> Objects.equals(rankList.get(i).getUnionUid(), unionUid)).findFirst();
		if (result.isPresent()) {
			return result.getAsInt() + 1;
		}
		return -1;
	}

	public void removeRank(String unionUid) {
		if (StringUtils.isNullOrEmpty(unionUid)) {
			return;
		}
		synchronized (this) {
			Iterator<CrossUnionActivityRank> iterator = rankList.iterator();
			while(iterator.hasNext()){
				CrossUnionActivityRank rank = iterator.next();
				if(unionUid.equals(rank.getUnionUid())){
					rank.setValue(BigInteger.ZERO);
					rank.setDel(true);
					rank.setUpdateOption();
					break;
				}
			}
			rankList.sort(sorter);
			initRankMsg();
			isUpdate = true;
			isNeedSync = true;
		}
	}

	/**
	 * 添加到排行
	 */
	public boolean rankChange(CrossUnionActivityRank unionRank) {
		if(null == unionRank){
			return false;
		}
		boolean isChange = false;
		synchronized (this) {
			int size = rankList.size();
			if (!rankList.stream().anyMatch(predicate -> Objects.equals(predicate.getUnionUid(), unionRank.getUnionUid()))) {
				if (size < rankNum) {
					CrossUnionActivityRank unionRankTemp = unionRank.clone();
					unionRankTemp.setLastUpdateTime(System.currentTimeMillis() / 1000);
					rankList.add(unionRankTemp);
					isChange = true;
				} else if (rankList.getLast().getValue().compareTo(unionRank.getValue()) < 0) {
					CrossUnionActivityRank rank = rankList.getLast();
					rank.setUnionUid(unionRank.getUnionUid());
					rank.setUnionBaseInfo(unionRank.getUnionBaseInfo());
					rank.setValue(unionRank.getValue());
					rank.setActivityId(unionRank.getActivityId());
					rank.setServerId(unionRank.getServerId());
					rank.setLastUpdateTime(System.currentTimeMillis() / 1000);
					rank.setDel(false);
					isChange = true;
				}
			} else {
				rankList.forEach(action -> {
					if (Objects.equals(action.getUnionUid(), unionRank.getUnionUid())) {
						action.setUnionBaseInfo(unionRank.getUnionBaseInfo());
						action.setValue(unionRank.getValue());
						action.setLastUpdateTime(System.currentTimeMillis() / 1000);
						action.setServerId(unionRank.getServerId());
						action.setActivityId(unionRank.getActivityId());
					}
				});
				isChange = true;
			}
            if(isChange){
                rankList.sort(sorter);
                initRankMsg();
                isNeedSync = true;
                isUpdate = true;
            }
		}
		return isChange;
	}

	public void saveActivityRankList() {
		LinkedList<CrossUnionActivityRank> newRankList;
		synchronized (this) {
			if (!isUpdate) {
				return;
			}
			newRankList = new LinkedList<>(rankList);
			isUpdate = false;
		}
		RankBussiness.updateCrossUnionActivityRankList(newRankList);
	}
}
